/*
 *@Type SocketClientUsage.java
 * @Desc
 * @Author urmsone urmsone@163.com
 * @date 2024/6/13 14:07
 * @version
 */
package example;

import client.Client;
import client.SocketClient;
import service.Node;
import utils.ConfigLoader;

import java.io.IOException;
import java.util.List;

public class SocketClientUsage {
    public static void main(String[] args) {

        try {
            // 1. 加载集群配置
            String configPath = "cluster.properties";
            List<Node> nodes = ConfigLoader.loadNodes(configPath);

            // 2. 创建客户端（自动连接到主节点）
            Client client = new SocketClient(nodes);

            // 3. 测试主从同步
//            System.out.println("=== 测试1: 写入主节点 ===");
//            client.set("user","zhangsan", "25");
//            client.set("user","lisi", "23");
//            client.set("user","wangwu", "18");
//            client.set("user","zhaoliu", "17");
//            client.set("user","zhouqi", "hello waaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
//            client.set("user","binge", "hello woooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
//            client.set("user","shili", "27");

//            client.set("user","u001","hello");
//            client.set("user","u002","hello world");
            client.set("user","u003","hello world");
            client.set("user","u004","hello world");
//            client.get("user","u001");

//            client.get("user", "zhangsan");

//            client.set("test_key2", "MS test2");
//            client.set("test_key3", "MS test3");

//            client.rm("z15");
//            client.get("z15");

//            Object value1 = client.get("test_key1");
//            Object value2 = client.get("test_key2");
//            Object value3 = client.get("test_key3");
//            System.out.println("客户端读取结果: " + value1);
//            System.out.println("客户端读取结果: " + value2);
//            System.out.println("客户端读取结果: " + value3);


            // 5. 测试删除同步
//            System.out.println("\n=== 测试2: 删除操作 ===");
//            client.rm("test_key");




//        client.set("zzy32","test b1");
//        client.set("z2","test2");
//        client.get("zzy32");
//        client.rm("z2");

//        client.set("z8", "abc8");
//        client.set("z9", "abc9");

//            client.get("z8");
//        client.get("z9");

//        client.set("z10","abc10");
//        client.get("z10");
//        client.rm("z10");

//        client.rm("z9");

//        client.set("z11", "zeus11");
//        client.get("z11");

            //List类型测试

//        ArrayList<Integer> list = new ArrayList<>();
//        Collections.addAll(list, 1, 2, 3, 4, 5);
//        client.set("z3",list);
//        Object o = client.get("z3");
//        List<Integer> list = JSONArray.parseArray(o.toString()).toJavaList(Integer.class);
//        System.out.println(list);
//        System.out.println(list.getClass().getName());

            //Map类型测试
//        TreeMap<String, Object> map = new TreeMap<>();
//        map.put("a1","test1");
//        map.put("a2","test2");
//        map.put("a3","test3");
//        client.set("z4", map);

//        Object o = client.get("z4");
//        System.out.println(o);


            //测试批量增
//        Map<String, Object> map = new TreeMap<>();
//        map.put("z5","Z555");
//        map.put("z6","Z666");
//        map.put("z7","Z777");
//
//        client.mSet(map);

            //测试批量改
//        Map<String, Object> map = new TreeMap<>();
//        map.put("z5","zzy555");
//        map.put("z6","zzy666");
//        map.put("z7","zzy777");
//
//        client.mSet(map);

//        client.get("z5");
//        client.get("z6");
//        client.get("z7");

            //测试批量删
//        ArrayList<String> list = new ArrayList<>();
//        Collections.addAll(list,"z5","z6","z7");
//
//        client.mRemove(list);
//        client.rm("z1");
//        client.get("z1");


        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}